<?php

namespace App\Models;

use App\Models\Rbac\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class OrderChild extends BaseModel
{
    use HasFactory;
    protected $table = 'order_child';
    // 允许批量操作的字段
    protected $fillable = [
        'id',
        'order_id',
        'job_type_id',
        'operator_user_id',
        'status',
        'is_epiboly',
        'submit_time',
        'created_at',
        'updated_at',
        'appoint_time',
        'epiboly_time',
        'price',
        'is_del',
        'is_appoint',
        'employee_user_id',
        'draw_time',
        'sort',
        'image1',
        'image2',
        'image3',
        'image4',
        'order_second_id',
        'product_num'
    ];

    public function scopeSearch($query,$params)
    {
        //订单日期（默认当月）

        //$start_time = $params['start_time']??date("Y-m-01 00:00:00");
        //$end_time = $params['end_time']??date("Y-m-d 23:59:59",strtotime("$start_time +1 month -1 day"));
        //$query->whereBetween('submit_time',[$start_time,$end_time]);
        if(!empty($params['start_time']) && !empty($params['end_time'])){
            $query->whereBetween('submit_time',[$params['start_time'],$params['end_time']]);
        }
        if(!empty($params['employee_user_id'])){
            $query->where('employee_user_id',$params['employee_user_id']);
        }
        if(!empty($params['job_type_id'])){
            $query->where('job_type_id',$params['job_type_id']);
        }
        if(!empty($params['is_epiboly'])){
            $query->where('is_epiboly',$params['is_epiboly']);
        }
        return $query;
    }

    public function order(){
        return $this->belongsTo(Order::class,'order_id')
            ->with('product');
    }

    public function job_type(){
        return $this->hasOne(JobType::class,'id','job_type_id');
    }

    public function employee(){
        return $this->hasOne(Employee::class,'user_id','employee_user_id');
    }

    public function image1(){
        return $this->hasOne(File::class,'id','image1');
    }

    public function image2(){
        return $this->hasOne(File::class,'id','image2');
    }

    public function image3(){
        return $this->hasOne(File::class,'id','image3');
    }

    public function image4(){
        return $this->hasOne(File::class,'id','image4');
    }

    public function order_second(){
        return $this->belongsTo(OrderSecond::class,'order_second_id','id');
    }



}
